package com.test38_集合;

import com.test38_集合.util.ILink;
import com.test38_集合.util.LinkImpl;
import org.junit.Test;

import java.util.*;

/**
 * 集合输出
 */
public class Demo03_Iterator {

    /**
     * forEach方法输出
     */
    @Test
    public void test1() {
        List<String> all = new ArrayList<>();
        all.add("www.yootk.com");
        all.add("edu.yootk.com");
        all.add("yootk.ke.qq.com");
        all.forEach(System.out::println);
    }

    /**
     * Iterator迭代输出
     */
    @Test
    public void test2() {
        List<String> all = new ArrayList<>();
        all.add("www.yootk.com");
        all.add("edu.yootk.com");
        all.add("yootk.ke.qq.com");
        Iterator<String> iter = all.iterator();
        while (iter.hasNext()) {    // 判断是否有下一个数据
            String str = iter.next();   // 获取数据
            if ("www.yootk.com".equals(str)) {
                // all.remove(str);    // 会报错
                iter.remove();
            }
            // System.out.println(str);
        }
        System.out.println(all);
    }

    /**
     * ListIterator双向迭代输出
     */
    @Test
    public void test3() {
        List<String> all = new ArrayList<>();
        all.add("www.yootk.com");
        all.add("edu.yootk.com");
        all.add("yootk.ke.qq.com");
        ListIterator<String> iter = all.listIterator();
        System.out.print("【由前向后输出】");
        while (iter.hasNext()) {
            String str = iter.next();
            System.out.print(str + "、");
            iter.set("沐言科技：" + str);
        }
        System.out.print("\n【由后向前输出】");
        while (iter.hasPrevious()) {
            System.out.print(iter.previous() + "、");
        }
    }

    /**
     * Enumeration枚举输出
     */
    @Test
    public void test4() {
        Vector<String> all = new Vector<>();    // 只有Vector才可以获取Enumeration
        all.add("www.yootk.com");
        all.add("edu.yootk.com");
        all.add("yootk.ke.qq.com");
        Enumeration<String> enumeration = all.elements();
        while (enumeration.hasMoreElements()) {
            System.out.println(enumeration.nextElement());
        }
    }

    /**
     * foreach输出
     */
    @Test
    public void test5() {
        List<String> all = Arrays.asList("www.yootk.com", "edu.yootk.com", "yootk.ke.qq.com");
        for (String message : all) {
            System.out.println(message);
        }
    }

    /**
     * 自定义foreach循环
     */
    @Test
    public void test6() {
        Book3 book = Book3.getInstance();
        for (Book3 temp : book) {
            System.out.println(temp);
        }
    }

    /**
     * 使用自定义链表
     */
    @Test
    public void test7() {
        ILink<String> all = new LinkImpl<>();
        all.add("www.yootk.com");
        all.add("edu.yootk.com");
        all.add("yootk.ke.qq.com");
        for (String data : all) {
            System.out.println(data);
        }
    }
}

class Book3 implements Iterable<Book3> {
    private static final Book3 INSTANCE = new Book3();
    private static final Book3[] ALL = new Book3[] {
            new Book3("Java从入门到项目实战"),
            new Book3("Python从入门到项目实战"),
            new Book3("Go从入门到项目实战")
    };
    private int foot = 0;   // 座位数组内容是否存在的计数
    private String title;

    private Book3() {
    }

    private Book3(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return "【Book】图书名称：" + this.title;
    }

    public static Book3 getInstance() {
        return INSTANCE;
    }

    @Override
    public Iterator<Book3> iterator() {
        return new BookIter();
    }

    /** 自定义的Iterator */
    private class BookIter implements Iterator<Book3> {
        @Override
        public boolean hasNext() {
            return Book3.this.foot < Book3.ALL.length;  // 有内容
        }

        @Override
        public Book3 next() {
            return Book3.ALL[Book3.this.foot++];    // 返回当前内容
        }
    }
}
